Subject: Re: Get WindowPtr of CLI-Windows?
From: [email-masked] (Michael B. Smith)
Date: Tue, 11 Jan 94 17:38:41 EDT
Newsgroups: comp.sys.amiga.programmer

In article <[email-masked]> [email-masked] writes:
> my problem is how to get the WindowPtr of the actual CLI-Window where my
> program is running in.
>
> It means:
> I have the FileHandlePtr to the IO-File:
>
> 1: MyFileHandle := Open(ADR("CON:"), newFile);
> or
> 2: MyFileHandle := Output();
>
> and need the WindowPtr of the Window open bye 'CON:' if there is an
> Outputwindow.
>
> My Language is Modula-II but if your example is in 'C', I can read it too.

This is an FAQ. Here is my solution:

/*
**	ConWindow.c
**
**	Michael B. Smith
**	February 2, 1993
**
**	No rights reserved. :)
*/

#include <exec/types.h>
#include <exec/ports.h>
#include <exec/memory.h>

#include <dos/dos.h>
#include <dos/dosextens.h>

#include <clib/dos_protos.h>
#include <clib/exec_protos.h>

#include <intuition/intuition.h>

struct MsgPort *
WhichPort (BPTR file)
{
	/*
	**  WhichPort
	**
	**	If file is passed and non-NULL, find the MsgPort of
	**	its handler process. If file is NULL, then if I am
	**	a process, return the MsgPort of my console handler.
	**
	**	See The AmigaDOS Manual, pp. 425 & 395, and
	**	AmigaMail II-24.
	*/

	struct MsgPort
		*rslt = NULL;

	if (file) {
		rslt = ((struct FileHandle *) (file << 2))->fh_Type;
	}
	else {
		struct Task *me = FindTask (NULL);

		if (me->tc_Node.ln_Type == NT_PROCESS)
			rslt = ((struct Process *) me)->pr_ConsoleTask;
	}

	return rslt;
}

struct Window *
WhichWindow (struct MsgPort *port)
{
	/*
	**  WhichWindow
	**
	**	Send a ACTION_DISK_INFO to the port passed in. It must be
	**	a message-port for a console handler. That will return a
	**	pointer to a filled-in "struct InfoData" and the window
	**	will be contained in the id_VolumeNode field.
	**
	**	Note that the window will be NULL for an AUTO or AUX console.
	*/

	struct InfoData
		*info;
	long
		rslt = 0;

	/* info must be long-word aligned */
	info = AllocVec (sizeof (struct InfoData), MEMF_PUBLIC);
	if (!info)
		return NULL;

	if (port) {
		rslt = DoPkt1 (port, ACTION_DISK_INFO, (long) (info >> 2));

		if (rslt) {
			rslt = info->id_VolumeNode;
		}
	}
	FreeVec (info);

	return (struct Window *) rslt;
}

int
main (int argc, char **argv)
{
	struct MsgPort
		*port;
	struct Window
		*mywin;

	/*
	**  If we get an argument, use Input (), otherwise find the default...
	*/

	/*
	**  You must be careful that Input() is from CON:. If not, results
	**  are undefined in this context...
	*/
	if (argc > 1) {
		printf ("Window from Input()\n");
		port = WhichPort (Input ());
	}
	else {
		printf ("Window from ConsoleTask\n");
		port = WhichPort (NULL);
	}

	if (port) {
		mywin = WhichWindow (port);
		if (!mywin)
			printf ("Couldn't find window\n");
		else {
			printf ("Left %ld, Top %ld,  Width %ld, Height %ld\n",
				mywin->LeftEdge, mywin->TopEdge,
				mywin->Width,	 mywin->Height);
		}
	}
	else {
		printf ("Couldn't find port\n");
	}

	exit (0);
}

--
  //   Michael B. Smith
\X/    [email-masked]  -or-  uunet.uu.net!virginia.edu!adastra!mbs